using UnityEngine;
using System.Collections;

public class MeleeDamage : MonoBehaviour {
	public bool debug = true;
	public int damage;
	public float range;
	
	NetworkEntityMgr nmgr;
	// Use this for initialization
	void Start () {
		nmgr = (NetworkEntityMgr) Object.FindObjectOfType(typeof(NetworkEntityMgr));
		
		if(gameObject.GetComponent<RigidbodyFPSController>()!=null){
			damage = gameObject.GetComponent<RigidbodyFPSController>().GetDamage();
		}
		else{
			damage = 30;	
		}

	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	void OnTriggerEnter (Collider other) {
		Debug.Log ("melee collision");
		if(gameObject.transform.parent.gameObject.layer == LayerMgr.LAYER_WEAPON){
			if(debug)Debug.Log ("Hit something");
			if(other.gameObject.GetComponent<Damagable>() != null){
				//ContactPoint contact = other.contacts[0];
				Vector3 pointything = gameObject.transform.position + gameObject.transform.forward * range;
				//other.gameObject.GetComponent<Damagable>().Damage(damage, pointything, -gameObject.transform.forward);
				int scorer = -1;
				WeaponSystem[] weaponSystems = Object.FindObjectsOfType(typeof(WeaponSystem)) as WeaponSystem[];
				PlayerState pl = null;
				foreach(WeaponSystem wep in weaponSystems){
					if(wep.enabled){
					Debug.Log("nid: "+wep.gameObject.GetComponent<NID>().getId());
					scorer = wep.gameObject.GetComponent<NID>().getId();
					pl = wep.gameObject.GetComponent<PlayerState>();
					}
				}
				other.networkView.RPC("MeleeDamageRPC", RPCMode.Others, damage, pointything,scorer);
				
				if(pl	!=	null)
					Listeners.inst().notify(Channel.ON_FIGHT,pl);
			}else{
				
				Listeners.inst().notify(Channel.ON_FIGHT,gameObject.GetComponent<PlayerState>());
				
				
//				if (other.GetComponent<Damagable>().life <= 0) {	
//					// Broadcast new score.
//					//HACK: Score needs to be fixe, thought: just send 1 and then all others just increase the score by the receive value instead of allways sending your total score
////					WeaponSystem[] weaponSystems = Object.FindObjectsOfType(typeof(WeaponSystem)) as WeaponSystem[];
////					foreach(WeaponSystem wep in weaponSystems){
////						if(wep.enabled){
////							Debug.Log("nid: "+wep.gameObject.GetComponent<NID>().getId());
////							ScoreListener.inst().notify(new ScoreEvent(wep.gameObject.GetComponent<NID>().getId(),ScoreAction.UPDATE,1));
////						}
////					}
//					
////					if (other.GetComponent<Entity>() == null) {
////						Listeners.inst().notify(Channel.ON_DAMAGE, other.GetComponent<PlayerState>());
////					} else {
////						Listeners.inst().notify(Channel.ON_DAMAGE, other.GetComponent<Entity>().getOwner().GetComponent<PlayerState>());
////					}
////					nmgr.networkView.RPC(
////						"rpcUpdateScore", 
////						RPCMode.All, 
////						networkView.viewID,
////						1
////					);
//								
//				}
				
				if (debug) Debug.Log ("And it was damagable");
			}
		}
	}
	
	public void SetDamage(int newer){
		damage = newer;	
		Debug.Log("Managed to set the new damage value");
	}
}
